#!/bin/sh


die()
{
    echo >& $1
    exit 1
}

x() 
{ 
    echo $*
    $* 
}



print_pkginfo()
{
    
    __pstamp=`fgrep $LOGNAME /etc/passwd | cut -f5 -d":"`
    cat <<- EOF
PKG="$1"
NAME="$2"
`[ "$OS" = "SunOS" ] && echo ARCH=sparc`
VERSION=`gmake -s version`
CATEGORY="application,graphics,opengl"
DESC="$3"
BASEDIR="/opt/"
VENDOR="OpenSceneGraph"
EMAIL="info@openscenegraph.org"
PSTAMP=$__pstamp
URL="http://openscenegraph.org"
MAXINST="1"
EOF

}

print_prototype()
{
    echo "i pkginfo"
    [ -f depend ] && echo "i depend"

    for d in $*
    do
	d=`echo $d | sed 's/\/opt\///` 
        echo "d none $d ? root sys"
    done

    for d in $*
    do
	for f in `ls -1 $d`
	do 
	     f=$d/$f
	     if [ -f $f ] 
	     then
 		[ -x $f ] && MODE=0555 || MODE=0444
		dest=`echo $f | sed 's/\/opt\///'`
	        echo "f none $dest=$f $MODE root sys"
	     fi
	done
    done

}

make_installable()
{
    PKGNAME=$1
    PKGDIR=$PKGNAME.pkg
    DISTNAME="$PKGNAME"-"`gmake -s version`"-"$OS".shar

    THISDIR=`pwd`
    cd $PKGSUBDIR

    cat << INSTALL_EOF > $DISTNAME

#!/bin/sh
cat <<-	 'LEOF' | uudecode -p | gzip -d -c | tar xvf -
`tar cvf - $PKGDIR | gzip | uuencode .tmpuu`
LEOF

/bin/sh << EOF
echo =========== INSTALLING  $DISTNAME =================
pkgadd -d $PKGNAME.pkg $PKGNAME
rm -rf $PKGDIR
EOF


INSTALL_EOF

    chmod +x $DISTNAME
    cd $THISDIR
}


make_package()
{
    PKGNAME=$1
    PKGDIR=$PKGSUBDIR/$PKGNAME.pkg
    x rm -rf $PKGDIR
    x mkdir  $PKGDIR

    echo Making pkginfo ...
    print_pkginfo "$1" "$2" "$3"  > $PKGDIR/pkginfo

    shift; shift; shift

    echo Making prototype ...

    print_prototype  $*  > $PKGDIR/prototype

#    DISTNAME="$PKGNAME"-"`$TOPDIR/bin/osgversion`"-"$OS"-"local"

    THISDIR=`pwd`
    x cd $PKGDIR
    x pkgmk -d .
    x cd $THISDIR
#    make_installable $PKGNAME $PKGDIR $DISTNAME 
#    x pkgtrans -s $PKGDIR $DISTNAME $PKGNAME
#    x mv "$PKGDIR"/"$DISTNAME" "$PKGDIR"/..
}


intro()
{
    echo; echo;
    echo "============ Building the $OS Distribution Package ==================="
    echo ""
    echo "    Please note that this script requires that" 
    echo 
    echo "        'make install' "
    echo 
    echo "    be previously run.  Please run "
    echo 
    echo "        'make distribution' "
    echo 
    echo "    with the same arguments (if any) as 'make install'"
    echo
    repeat=1
    while [ $repeat = 1 ] 
    do 
      printf "    Continue? (y/n) [y]: "
      read resp
      case $resp in  
       ""|Yes|YES|yes|Y|y) repeat=0 ;; 
	        n|N|no|NO) return 1; repeat=0;;
                *)         repeat=1;; 
      esac
   done

   return 0
}

intro || exit 0

OS=$1
TOPDIR=$2
INST_LIBS=$3
INST_PLUGINS=$4
INST_INCLUDE=$5
INST_EXAMPLES=$6
INST_SRC=$7
INST_EXAMPLE_SRC=$8
shift;shift; shift;shift; shift;shift; shift;shift;
INST_DOC=$1
INST_DATA=$2


PKGSUBDIR=dist/PKG

make_package\
	"OpenSceneGraph" \
	"Open Scene Graph - Run-time environment"\
	"An OpenGL based scene graph. This installation includes a set of run-time libraries, and example programs"\
        $INST_LIBS $INST_PLUGINS $INST_EXAMPLES\

make_installable "OpenSceneGraph"

#
# Capture include directories
#
SUBDIRS=`ls $TOPDIR/include | grep -v CVS`
INSTARG=""
for d in $SUBDIRS
do
    dd="$INST_INCLUDE"/"$d"
    [ -d $dd ] && INSTARG="$INSTARG $dd"
done

#
# Capthre examples subdirectories
#
SUBDIRS=`ls $TOPDIR/src/examples | grep -v CVS`
for d in $SUBDIRS
do
    dd="$INST_EXAMPLE_SRC"/"$d"
    [ -d $dd ] && INSTARG="$INSTARG $dd"
done

#
# Capture Make subdirectory
#
dd="$INST_SRC"/"Make"

echo dd is $dd
ls $dd

[ -d $dd ] && INSTARG="$INSTARG $dd"


make_package\
	"OpenSceneGraph-dev"\
	"Open Scene Graph - Development environment"\
	"An OpenGL based scene graph. This installation includes header files and example program source files"\
	$INSTARG

make_installable "OpenSceneGraph-dev"


exit 0
